import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'welcome to flutter',
      theme: ThemeData(primaryColor: Colors.white),
      home: Scaffold(
        appBar: AppBar(
          title: Text('抖动'),
          centerTitle: true,
          leading: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              IconButton(
                color: Colors.blue,
                icon: Icon(Icons.arrow_back_ios_rounded),
                tooltip: '动画',
                onPressed: () {
                  print('点击了返回');
                },
              ),
              Positioned(
                left: 28.0,
                child: Container(
                  child: Text(
                    '动画',
                    style: TextStyle(color: Colors.blue),
                  ),
                ),
              ),
            ],
          ),
          // ),
        ),
        body: ContentPage(),
      ),
    );
  }
}

class ContentPage extends StatefulWidget {
  @override
  _ContentPageState createState() => _ContentPageState();
}

class _ContentPageState extends State<ContentPage>
    with SingleTickerProviderStateMixin {
  String butText = '开始';
  Animation<double> animation; //动画对象
  AnimationController controller; //动画控制器

  @override
  initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 150), vsync: this);
    animation = Tween(begin: 0.0, end: 0.1).animate(controller);
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        //监听动画完成状态
        controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //监听动画结束的状态
        controller.forward();
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void _onPressOrClick() {
    //判断是都在抖动
    // print(controller.isAnimating);
    if (controller.isAnimating) {
      this.setState(() {
        butText = '开始';
      });
      controller.stop();
    } else {
      this.setState(() {
        butText = '暂停';
      });
      controller.forward();
    }
    ;
  }

  Widget build(BuildContext context) {
    var duration = Duration(seconds: 5);
    return ConstrainedBox(
      constraints: BoxConstraints.expand(),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            top: 60.0,
            child: RotationTransition(
              turns: animation,
              child: Container(
                constraints:
                    BoxConstraints.tightFor(width: 100.0, height: 100.0),
                decoration: BoxDecoration(color: Colors.red),
              ),
            ),
          ),
          Positioned(
            bottom: 100.0,
            child: FlatButton(
              color: Colors.orange,
              minWidth: 130.0,
              padding: EdgeInsets.all(20.0),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(2.0)),
              child: Text(
                this.butText,
                style: TextStyle(color: Colors.white),
              ),
              //长按事件
              onLongPress: () {
                _onPressOrClick();
              },
              //点击事件
              onPressed: () {
                _onPressOrClick();
              },
            ),
          ),
        ],
      ),
    );
  }
}
